﻿using Kingdee.BOS.Core.DynamicForm.PlugIn;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DevSamPle.FormPlugin
{
    [Description("【表单插件】单据获取种子值"), HotUpdate]
    public class GetSeqValue : AbstractDynamicFormPlugIn
    {
        public override void BarItemClick(BarItemClickEventArgs e)
        {
            base.BarItemClick(e);
            if (e.BarItemKey.Equals("GetSeqValue", StringComparison.OrdinalIgnoreCase))
            {
                // 获取表的种子值=获取表的自增Id=获取表的自增主键值，每获取一次，种子值就会加1，以确保主键值不会重复产生
                // 种子值不是来自于当前数据表，而是来自于一个单独的种子表，一个数据表对应一个种子表，种子表名通常为数据表名的首字母t改为z，例如，数据表t_PUR_POOrder对应的种子表名为z_PUR_POOrder
                var log = new StringBuilder();
                // 取单据头
                var tableName = this.View.BusinessInfo.GetEntity("FBillHead").TableName;
                //var seqValue = new SequenceReader(this.Context).GetSequence(tableName, 1).FirstOrDefault();// 服务插件用这个方法
                var seqValue = DBServiceHelper.GetSequenceInt32(this.Context, tableName, 1).FirstOrDefault(); // 表单插件用这个方法
                log.Append(string.Format("表名：{0}，种子值(内码)：{1}", tableName, seqValue));
                log.AppendLine();
                // 取单据头多语言表
                tableName = this.View.BusinessInfo.GetEntity("FBillHead").TableName;
                tableName = tableName + "_L";
                seqValue = DBServiceHelper.GetSequenceInt32(this.Context, tableName, 1).FirstOrDefault();
                log.Append(string.Format("表名：{0}，种子值(内码)：{1}", tableName, seqValue));
                log.AppendLine();
                // 取单据体
                tableName = this.View.BusinessInfo.GetEntity("FPOOrderEntry").TableName;
                seqValue = DBServiceHelper.GetSequenceInt32(this.Context, tableName, 1).FirstOrDefault();
                log.Append(string.Format("表名：{0}，种子值(内码)：{1}", tableName, seqValue));
                log.AppendLine();
                // 取子单据体
                tableName = this.View.BusinessInfo.GetEntity("FEntryDeliveryPlan").TableName;
                seqValue = DBServiceHelper.GetSequenceInt32(this.Context, tableName, 1).FirstOrDefault();
                log.Append(string.Format("表名：{0}，种子值(内码)：{1}", tableName, seqValue));
                log.AppendLine();
                this.Model.SetValue("F_POSW_Remarks", log.ToString());
            }
        }
    }
}
